<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="${dao.namespace}">

	<sql id="query_column">
		<trim suffixOverrides=",">
			<#if table.columns??>
				<#list table.columns as c>
			<#comment>${"<!--"} ${c.field} ${"->"}<#if c_has_next></#comment>
			<if test="columns.${table.name}.${c.name?trim}">
				${table.name}.${c.name?trim},
			</if>
			<#rt/>
				</#list>
			</#if>
		</trim>
	</sql>

	<sql id="count_column">
		COUNT(${table.name}.${table.primary})
	</sql>

	<sql id="query_table">
		${table.name}
	</sql>

	<sql id="where_clause">
		<where>
			<if test="and_clause and and_clause.size > 0">
				<foreach collection="and_clause" item="i" separator="AND">
					<choose>
					<#noparse>
						<when test="i.assert">
							${i.column} ${i.operator}
						</when>
						<when test="i.single">
							${i.column} ${i.operator} #{i.value}
						</when>
						<when test="i.between">
							${i.column} ${i.operator} #{i.begin} AND #{i.end}
						</when>
						<when test="i.list">
							${i.column} ${i.operator}
							<foreach collection="i.value" item="v" open="(" separator="," close=")">
								#{v}
							</foreach>
						</when>
					</#noparse>
					</choose>
				</foreach>
			</if>
			<if test="or_clause and or_clause.size > 0">
				<foreach collection="or_clause" item="i" open="OR (" separator="OR" close=")">
					<choose>
					<#noparse>
						<when test="i.assert">
							${i.column} ${i.operator}
						</when>
						<when test="i.single">
							${i.column} ${i.operator} #{i.value}
						</when>
						<when test="i.between">
							${i.column} ${i.operator} #{i.begin} AND #{i.end}
						</when>
						<when test="i.list">
							${i.column} ${i.operator}
							<foreach collection="i.value" item="v" open="(" separator="," close=")">
								#{v}
							</foreach>
						</when>
					</#noparse>
					</choose>
				</foreach>
			</if>
		</where>
	</sql>

	<sql id="group_by_clause">
		<if test="group_by_clause">
			GROUP BY
		</if>
	</sql>

	<sql id="having_clause">
		<if test="having_clause">
			HAVING
		</if>
	</sql>

	<sql id="order_by_clause">
		<if test="order_by_clause">
			ORDER BY
		</if>
	</sql>

	<sql id="limit_clause">
		<#noparse><if test="start and limit">LIMIT ${start}, ${limit}</if></#noparse>
	</sql>



	<!-- Statements -->
	<select id="query" parameterType="hashmap" resultType="hashmap">
		SELECT
		<include refid="query_column" />
		FROM
		<include refid="query_table" />
		<include refid="where_clause" />
		<include refid="group_by_clause" />
		<include refid="having_clause" />
		<include refid="order_by_clause" />
		<include refid="limit_clause" />
	</select>

	<select id="count" parameterType="hashmap" resultType="int">
		SELECT
		<include refid="count_column" />
		FROM
		<include refid="query_table" />
		<include refid="where_clause" />
		<include refid="group_by_clause" />
		<include refid="having_clause" />
	</select>

	<insert id="insert" parameterType="hashmap"<#if table.primary_count == 1> useGeneratedKeys="true" keyProperty="${table.primary}"</#if>>
		INSERT INTO ${table.name}
		<trim prefix="(" suffix=")" suffixOverrides=",">
			<#if table.columns??>
				<#list table.columns as c>
			<if test="${c.name}">${c.name},</if>
				</#list>
			</#if>
		</trim>
		VALUES
		<trim prefix="(" suffix=")" suffixOverrides=",">
			<#if table.columns??>
				<#list table.columns as c>
			<#comment><if test="${c.name}">${"#"}{${c.name},jdbcType=${c.type}},</if></#comment>
			<if test="${c.name}">${"#"}{${c.name}},</if>
				</#list>
			</#if>
		</trim>
	</insert>

	<update id="update" parameterType="hashmap">
		UPDATE ${table.name}
		<set>
			<#if table.columns??>
				<#list table.columns as c>
			<if test="${c.name}">${c.name} = ${"#"}{${c.name}},</if>
				</#list>
			</#if>
		</set>
		<include refid="where_clause" />
	</update>

	<delete id="delete" parameterType="hashmap">
		DELETE FROM ${table.name}
		<include refid="where_clause" />
	</delete>

</mapper>